﻿Imports System.Data.Linq
Imports System.Linq

Public Class MGoods
    Private db As New TanNamChinhDataContext
    Public Function getGoodsList1() As Table(Of tblGood)
        Try
            Return db.tblGoods
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getGoodsList2() As IQueryable(Of tblGood)
        Try
            Dim query = From b In db.tblGoods
                        Where b.IsActive = True
                        Order By b.ID_Good Descending
                        Select b
            'Return (From r In db.tblGoods
            '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getGoodsList3() As IQueryable
        Try
            Dim query = From b In db.tblGoods
                        Where b.IsActive = True
                        Order By b.ID_Good Descending
                        Select b.ID_Good, b.GoodName, b.Description
            'Return (From r In db.tblGoods
            '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getGoodsList3(ByVal goods As String) As IQueryable
        Try
            Dim query = From b In db.tblGoods
                        Where b.IsActive = True And b.GoodName.Contains(goods)
                        Order By b.ID_Good Descending
                        Select b.ID_Good, b.GoodName, b.Description
            'Return (From r In db.tblGoods
            '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub InsertNewRecord(ByVal obj As tblGood)
        db.tblGoods.InsertOnSubmit(obj)
        'db.GetTable(Of tblGood)().InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub
    Public Sub UpdateRecord(ByVal obj As tblGood)
        Dim getData = (From b In db.tblGoods
                                Where b.ID_Good = obj.ID_Good
                                Select b).Single()
        getData.GoodName = obj.GoodName
        getData.Description = obj.Description
        getData.Description = obj.Description
        db.SubmitChanges()

    End Sub
    Public Sub DeleteUpdateRecord(ByVal id As Integer)
        Dim getData = (From b In db.tblGoods
                                Where b.ID_Good = id
                                Select b).Single()
        getData.IsActive = False
        db.SubmitChanges()

    End Sub
    Public Sub DeleteRecord(ByVal id As Integer)
        Dim getData = (From b In db.tblGoods
                                Where b.ID_Good = id
                                Select b).Single()
        db.tblGoods.DeleteOnSubmit(getData)
        db.SubmitChanges()
    End Sub
End Class
